package development.group;

import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.Graphics;
import state.GroupForm;
import state.Key;
import state.component.BottomBar;
import state.component.Event;
import state.component.EventListener;
import state.component.GameMenu;
import state.component.MultiTab;
import state.util.Hashtable;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.FrmChat;
import development.FrmChooseApp;
import development.FrmChooseGame;
import development.FrmGameServiceList;
import development.FrmLogin;
import development.FrmUserInfo;
import development.Global;
import development.socket.SocketClientUtil;

public class FrmGroupMenu extends GroupForm implements EventListener {
  private MultiTab multiTab;
  public static int tabIndex;
  private GameMenu menu;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmGroupMenu();
    }
    return singleton;
  }
  
  public FrmGroupMenu() {
  }

  public void init(Hashtable hashtable) {
    SocketClientUtil.requestUserInfo(Global.currentUser.getName());
    SocketClientUtil.requestDownloadUrl();
    
    multiTab = new MultiTab(this, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
    multiTab.addTab("WinPlay", FrmChooseApp.instance());
    multiTab.addTab("Trò chơi", FrmChooseGame.instance());
    multiTab.addTab("Phòng chat", FrmChat.instance());
    multiTab.setStyle(Constants.MULTI_TAB_STYLE);
    ((FrmChooseApp) FrmChooseApp.instance()).getGridMenu().setNextTabWhenEndRow(multiTab);
    ((FrmChooseGame) FrmChooseGame.instance()).getGridMenu().setBackTabWhenBeginRow(multiTab);
    ((FrmChooseGame) FrmChooseGame.instance()).getGridMenu().setNextTabWhenEndRow(multiTab);
    multiTab.selectTab(tabIndex);
    
    // Create bottom bar
    menu = new GameMenu(null, new String[] {"Chuyển server", "Sự kiện", "Bạn bè", "Tặng game", "Nạp xu", "Đổi gold", "Góp ý", "Diễn đàn", "Thông tin TK", "Đăng xuất"}, 10, this, Constants.MENU_STYLE, 0, GameConstants.SCREEN_HEIGHT - 45);
    BottomBar bottomBar = DrawUtils.getBottomBar(_manager);
    bottomBar.setLabels("Menu", "Chọn", "");
    bottomBar.setLeftMenu("Menu", menu);
    Global.checkToDisplayAddFriendRequest();
    SocketClientUtil.requestNumberOfUserOnline();
  }
  
  public MultiTab getMultiTab() {
    return multiTab;
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    DrawUtils.getBottomBar(_manager).setLabels("Menu", "Chọn", "");
    DrawUtils.getBottomBar(_manager).setLeftMenu("Menu", menu);
  }
  
  public void goBack() {
  }
  
  public void onActionPerform(Event event) {
    tabIndex = multiTab.getCurrentSelectedTab();
    // "Sự kiện", "Tặng game", "Nạp xu", "Đổi gold", "Góp ý", "Diễn đàn", "Cài đặt", "Thông tin TK", "Đăng xuất"
    String action = event.getAction();
    if (action.equals("Chuyển server")) {
      Global.nextState(FrmGameServiceList.instance(), null);
    } else if (action.equals("Sự kiện")) {
      FrmGroupThoiTietTinTuc.tabIndex = 2;
      Global.nextState(FrmGroupThoiTietTinTuc.instance(), null);
    } else if (action.equals("Tặng game")) {
      // TODO: FrmSendGame
//      Global.nextState(FrmSendGame.instance(), null);
    } else if (action.equals("Bạn bè")) {
      GameGlobal.nextState(FrmGroupFriendAndTop.instance(), null);
      FrmGroupFriendAndTop.backForm = FrmGroupMenu.instance();
    } else if (action.equals("Nạp xu")) {
      FrmGroupNapXuAndExchangeGold.backForm = this;
      Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), null);
    } else if (action.equals("Đổi gold")) {
      FrmGroupNapXuAndExchangeGold.backForm = this;
      Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), null);
      FrmGroupNapXuAndExchangeGold.setTab(1);
    } else if (action.equals("Góp ý")) {
      // TODO: FrmFeedback
//      Global.nextState(FrmFeedback.instance(), null);
    } else if (action.equals("Diễn đàn")) {
      Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create1("Bạn có muốn đến", "diễn đàn WinPlay?"), 98);
    } else if (action.equals("Thông tin TK")) {
      FrmUserInfo.goBackForm = this;
      Global.nextState(FrmUserInfo.instance(), null);
    } else if (action.equals("Đăng xuất")) {
      GameGlobal.alert.showAlert2(this, GameAlert.YES_NO_TYPE, "Bạn có muốn đăng xuất?", 99);
    }
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    tabIndex = multiTab.getCurrentSelectedTab();
    if (alertId == 99 && alertType == GameAlert.YES_NO_TYPE && eventType == GameAlert.YES_BUTTON) { // Thoát ra
      SocketClientUtil.signOut();
      GameGlobal.nextState(FrmLogin.instance(), null);
    } else if (alertId == 98 && alertType == GameAlert.YES_NO_TYPE && eventType == GameAlert.YES_BUTTON) { // Đến diễn đàn
      Global.systemCanvas.getMidlet().platformRequest("http://www.winplay.com.vn/forum");
    }
  }
  
  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.LEFT:
      break;
    case Key.RIGHT:
      break;
    case Key.SOFT_LEFT:
      break;
    case Key.SOFT_RIGHT:
      multiTab.previousTab();
      break;
    }
  }
  
  public void destroy() {
    tabIndex = multiTab.getCurrentSelectedTab();
    multiTab.detroy();
    multiTab = null;
    menu = null;
    FrmChooseApp.instance().detroyAll();
    FrmChooseGame.instance().detroyAll();
  }
}
